
#pragma once

#include "functions.h"
#include "../ql/qlreader.h"

Obj *
f_get_namespace( void * tag, List * args )
{
    RuleEngineContext * r = tag;
    Id *                ns;
    size_t              len = list_length( args );

    if( len == 0 )
    {
        if( 0 == r->current_reader )
        {
            return g_nil_obj;
        }

        ns = get_default_namespace( r->current_reader );
    }
    else if( len > 1 || args->car->type != OBJ_ID )
    {
        evaluator_emit_error( r->eval, QL_ERR_INVALID_ARGUMENTS );
    }
    else
    {
        ns = ((Id *)args->car)->ns;
    }

    return (0 == ns ? g_nil_obj : (Obj *)ns);
}

Obj *
f_set_namespace( void * tag, List * args )
{
    RuleEngineContext * r = tag;
    Id *                previous_ns;

    if( list_length(args) != 1 || args->car->type != OBJ_ID )
    {
        evaluator_emit_error( r->eval, QL_ERR_INVALID_ARGUMENTS );
    }

    if( 0 == r->current_reader )
    {
        evaluator_emit_error( r->eval, QL_ERR_FAIL );
    }

    previous_ns = set_default_namespace( r->current_reader, (Id *)args->car );

    return (0 == previous_ns ? g_nil_obj : (Obj *)previous_ns);
}
